package gui.list_objects;

import javax.swing.table.AbstractTableModel;
import objects.Pays;

public class result_recherche_pays_model extends AbstractTableModel {

	private static final long serialVersionUID = 1L;
	private Object[][] data;
	private String[] title={"Pays"};

	public result_recherche_pays_model(Pays[] rawData){
		this.data=this.convert(rawData);
	}

	public void setNewData(Pays[] rawData){
		this.data=this.convert(rawData);
	}

	private Object [][] convert(Pays[] rawData){
		Object[][] result;
		if (rawData.length==0){
			result=new Object[1][2];
			result[0][0]="aucun résultat";	
			result[0][1]=-1;
		}
		else{
			result=new Object[rawData.length][2];
			for(int i=0;i<rawData.length;i++){
				result[i][0]=rawData[i].getNom();
				result[i][1]=rawData[i].getId_pays();
			}
		}

		return result;
	}
	@Override
	public int getColumnCount() {
		return this.title.length;
	}

	@Override
	public int getRowCount() {
		return this.data.length;
	}

	@Override
	public Object getValueAt(int row, int col) {
		return this.data[row][col];
	}

	public String getColumnName(int col) {
		return this.title[col];
	}
	
	public int getLineOfId(int id){
		for(int i=0;i<data.length;i++)
			if((int)data[i][1]==id)
				return i;
		return -1;
	}

}
